﻿// ***********************************************************************
// Assembly         : CtrLayer
// Author           : Kristian, Henrik
// Created          : 05-09-2014
//
// Last Modified By : Kristian
// Last Modified On : 05-12-2014
// ***********************************************************************

using DBLayer;
using DBLayer.Datacontext;
using ModelLayer;
using System;
using System.Linq;
using System.Collections.Generic;

namespace CtrLayer
{
    /// <summary>
    /// Class CtrUser.
    /// </summary>
    public class CtrUser : ICtrUser
    {
        /// <summary>
        /// Creates the user in the database.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns><c>true</c> if user is created, <c>false</c> otherwise.</returns>
        public int CreateUser(User user)
        {
            var dbUser = new DbUserOp();
            int result = dbUser.InsertUser(user);
            return result;
        }

        /// <summary>
        /// Gets the user from the database.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns>User.</returns>
        public User GetUser(int userId)
        {
            var dbUser = new DbUserOp();

            return dbUser.GetUser(userId);
        }

        /// <summary>
        /// Gets the user from the database.
        /// </summary>
        /// <param name="userEmail">The user email.</param>
        /// <returns>DBUser.</returns>
        public User GetUser(string userEmail)
        {
            var dbUser = new DbUserOp();
            return dbUser.GetUser(userEmail);
        }

        /// <summary>
        /// Gets the user from the database.
        /// </summary>
        /// <param name="userEmail">The user email.</param>
        /// <param name="userPassword">The user password.</param>
        /// <returns>DBUser.</returns>
        public User GetUser(string userEmail, string userPassword)
        {
            var dbUser = new DbUserOp();
            return dbUser.GetUser(userEmail, userPassword);
        }

        /// <summary>
        /// Author: Anders^2
        /// </summary>
        /// <param name="toConvert">The DBUser to be converted to a ModelLayer.Usery</param>
        /// <returns>The converted user</returns>
        public User ConvertToUser(DBUser toConvert)
        {
          if (toConvert != null)
          {
            return new User(toConvert.fName,
                            toConvert.lname,
                            toConvert.profileName,
                            toConvert.email,
                            toConvert.gender,
                            toConvert.password,
                            (ModelLayer.User.Status)((int)(toConvert.status)));
          }
          return null;
        }


        public List<User> GetFriendships(User user)
        {
            DbUserOp dbUser = new DbUserOp();

            return dbUser.GetFriendships(user);
        }


        public List<Friend> GetPendingFriendships(User user)
        {
            DbUserOp userOperations = new DbUserOp();
            return userOperations.GetPendingFriendships(user);
        }

        /*public List<User> GetPendingFriendships(User user)
        {
            DbUserOp userOperations = new DbUserOp();
            return userOperations.GetPendingFriendships(user);
        }*/


        public int RequestFriendship(User requester, User requested)
        {
            DbUserOp userOp = new DbUserOp();
            return userOp.RequestFriendship(requester, requested);


        }


        /// <summary>
        /// Author: Poul-Erik
        /// </summary>
        /// <param name="profilename"></param>
        /// <param name="firstname"></param>
        /// <param name="lastname"></param>
        /// <returns>Users with the above search-parameters.
        /// BEWARE: parameters are OR'ed, that is this is an OR-search</returns>
        public List<User> SearchUserList(string[] keywords)
        {
            return (new DbUserOp()).SearchUserList(keywords);
        } // SearchUsrList

        public int SetFriendshipStatus(User requester, User accepter, Friend.Status  status)
        {
            DbUserOp userOp = new DbUserOp();
            return userOp.ChangeFriendshipStatus(requester, accepter, status);
        }


        public int DeleteUsers(int[] userIDs)
        {
          DbUserOp op = new DbUserOp();
          return op.DeleteUsers(userIDs);
        }
    }
}